package me.learningandroid.ch3_1.ConfigFileIo;

import android.os.Bundle;
import android.util.Log;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.gson.Gson;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.stream.Collectors;

public class FileIoActivity extends AppCompatActivity {
    private static final String FILENAME="config";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fileio);

        EditText editText = findViewById(R.id.editText);
        RadioGroup radioGroup = findViewById(R.id.radioGroup);
        CheckBox checkBox = findViewById(R.id.checkBox);

        Button applyBtn = findViewById(R.id.applyBtn);
        Button saveBtn = findViewById(R.id.saveBtn);

        applyBtn.setOnClickListener(v -> {
            FileInputStream fileInputStream = null;
            BufferedReader bufferedReader = null;
            try {
                // `openFileInput(name)`打开文件输入流
                fileInputStream = openFileInput(FILENAME);
                // 输出流是用于输入字节的，用`Reader`包装后能够输入字符串
                bufferedReader = new BufferedReader(new InputStreamReader(fileInputStream));
                String content = bufferedReader.lines().collect(Collectors.joining());
                SaveObj saveObj = new Gson().fromJson(content, SaveObj.class);
                editText.setText(saveObj.text);
                radioGroup.check(saveObj.selectedRadioId);
                checkBox.setChecked(saveObj.checked);
                bufferedReader.close();
                Toast.makeText(this, "应用草稿成功", Toast.LENGTH_LONG).show();
            } catch (FileNotFoundException e) {
                Toast.makeText(this, "没有草稿", Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                Toast.makeText(this, "关闭bufferedWriter出错", Toast.LENGTH_LONG).show();
            } finally {
                if (bufferedReader!=null) {
                    try {
                        bufferedReader.close();
                    } catch (IOException e) {
                        Log.e(getLocalClassName(), "关闭bufferedWriter出错");
                    }
                }
                if (fileInputStream!=null) {
                    try {
                        fileInputStream.close();
                    } catch (IOException e) {
                        Log.e(getLocalClassName(), "关闭fileOutputStream出错");
                    }
                }
            }

        });

        saveBtn.setOnClickListener(v -> {
            String text = editText.getText().toString();
            int selectedRadioId = radioGroup.getCheckedRadioButtonId();
            boolean checked = checkBox.isChecked();
            FileOutputStream fileOutputStream=null;
            BufferedWriter bufferedWriter=null;
            try {

                // `openFileOutput(name， mode)`打开文件输出流
                fileOutputStream = openFileOutput(FILENAME, MODE_PRIVATE);
                // 输出流是用于输出字节的，用`Writer`包装后能够输出字符串
                bufferedWriter = new BufferedWriter(new OutputStreamWriter(fileOutputStream));
                bufferedWriter.write(new Gson().toJson(new SaveObj(text, selectedRadioId, checked)));
                bufferedWriter.close();
                Toast.makeText(this, "保存草稿成功", Toast.LENGTH_LONG).show();
            } catch (FileNotFoundException e) {
                Toast.makeText(this, "没有草稿", Toast.LENGTH_LONG).show();
            } catch (IOException e) {
                Toast.makeText(this, "读取草稿出错", Toast.LENGTH_LONG).show();
            } finally {
                if (bufferedWriter!=null) {
                    try {
                        bufferedWriter.close();
                    } catch (IOException e) {
                       Log.e(getLocalClassName(), "关闭bufferedWriter出错");
                    }
                }
                if (fileOutputStream!=null) {
                    try {
                        fileOutputStream.close();
                    } catch (IOException e) {
                        Log.e(getLocalClassName(), "关闭fileOutputStream出错");
                    }
                }
            }
        });

        FloatingActionButton nextBtn = findViewById(R.id.floatingActionButton);
        nextBtn.setOnClickListener(v -> {
            SharedPreferencesActivity.startActivity(this);
        });
    }

    private static class SaveObj {

        public String text;
        public int selectedRadioId;
        public boolean checked;

        public SaveObj(String text, int selectedRadioId, boolean checked) {
            this.text = text;
            this.selectedRadioId = selectedRadioId;
            this.checked = checked;
        }
    }


}